<template>
	<view>
		<dialog-shell title="用户登录" confirm-text="登录" ref="login" @confirm="login">
			<view class="login_container">
				<image src="../../static/icon/icon.png" mode="aspectFill" class="login_image"></image>
				WELL
				<view class="login_content">
					<input :value="usrValue" type="text" class="login_input" @input="getUsrValue">
					<text class="icon iconfont icon-user"></text>
				</view>
				<view class="login_content">
					<input :value="pwdValue" type="text" class="login_input" password="true" @input="getPwdValue">
					<text class="icon iconfont icon-password"></text>
				</view>
				<text id="regi" class="login_regi" @click="clickGoRegi">还没有账号?点此注册</text>
			</view>
		</dialog-shell>
	</view>
</template>

<script>
	export default {
		name: "login-dialog",
		data() {
			return {
				isPwd: false,
				usrValue: "",
				pwdValue: "",
			};
		},
		mounted() {
			let user = uni.getStorageSync(this.$params.key_user);
			if (user) {
				this.usrValue = user.username;
				this.pwdValue = user.password;
			}
		},
		methods: {
			show() {
				this.$refs.login.show();
			},
			hidden(){
				this.$refs.login.clickToClose();
			},
			getUsrValue(e) {
				this.usrValue = e.detail.value;
			},
			getPwdValue(e) {
				this.pwdValue = e.detail.value;
			},
			login() {
				if (this.usrValue.length == 0 || this.pwdValue.length == 0) {
					console.log(2222);
					uni.showToast({
						title: "请输入用户名或密码",
						icon: "none"
					})
					return;
				}
				let data = {
					"username": this.usrValue,
					"password": this.pwdValue,
				}
				this.$request.jsonParam(this.$params.host + this.$params.user_login, data, res => {
					if (!res.success) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						this.$refs.login.show();
						this.usrValue = "";
						this.pwdValue = "";
						return;
					}
					// uni.showToast({
					// 	title:res.message,
					// 	icon:'none'
					// })
					uni.setStorageSync(this.$params.key_user, data);
					let app = getApp();
					app.globalData.uid = res.data.id;
					app.globalData.token = res.data.token;
					app.globalData.avatar = res.data.avatar;
					app.globalData.type = res.data.type;
					app.globalData.nickName = res.data.nickName;
					app.globalData.userName = res.data.userName;
					app.globalData.email = res.data.email;
					// this.$emit("afterLogin");
					this.$noti.post(this.$params.noti_login_status,{});
				}, () => {})


			},
			clickGoRegi(){
				uni.navigateTo({
					url: '../../pages/register/register',
					success: res => {
						this.$refs.login.clickToClose()
					},
					fail: () => {},
					complete: () => {}
				});
			}

		},

	}
</script>

<style>
	@import url("login-dialog.css");
</style>
